//
//  HRDunamicLayout.m
//  Hear
//
//  Created by mac on 2017/4/9.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HRDunamicLayout.h"
#import "NSMutableAttributedString+GDAttString.h"
#import "HRFollowDynamicPhotoView.h"
@interface HRDunamicLayout()
@property (nonatomic, assign)CGFloat leftPading;

@end
@implementation HRDunamicLayout
- (instancetype)initWithStatus:(HRDynamicModel *)dynamicModel style:(HRLayoutStyle)style{
    self = [super init];
    _dynamicModel = dynamicModel;
    _style = style;
    [self layout];
    return self;
}

- (void)layout {
    if(_style == HRLayoutStyleTimeline){
        [self _layout];
    }
    
}


- (void)_layout{

    CGFloat leftPading = 10;
    self.leftPading = leftPading;
    
    _timeViewRect = CGRectMake(leftPading, leftPading, 120, 30);
    
    
    CGFloat backViewHeight = 0;
    CGFloat backViewWidth = kFullWidth-leftPading*2;
    
    if ([self layoutTitleHeight]){
        _contentRect = CGRectMake(leftPading, leftPading, backViewWidth-leftPading*2, [self layoutTitleHeight]);
    }else{
        _contentRect = CGRectMake(leftPading, 0, backViewWidth-leftPading*2, [self layoutTitleHeight]);
    }
    
    
    if (!_dynamicModel.Permission && ![_dynamicModel.UserLocalId isEqualToString:USERID]){ // 隐藏
        
        _lockImageViewRect = CGRectMake(leftPading,CGRectGetMaxY(_contentRect), backViewWidth-leftPading*2, backViewWidth-leftPading*2);
        
        _bottomViewRect = CGRectMake(0, CGRectGetMaxY(_lockImageViewRect)+leftPading, backViewWidth, 30);
        
        _contentViewRect = CGRectMake(leftPading, CGRectGetMaxY(_timeViewRect)+leftPading, backViewWidth, CGRectGetMaxY(_bottomViewRect));
        
        _cellHeight = CGRectGetMaxY(_contentViewRect)+leftPading;
        return;
    }
    
    

    if ([_dynamicModel.MomentType intValue] == 2 || [_dynamicModel.MomentType intValue] == 3){
        
        CGSize imageViewSize;
        
        if (_dynamicModel.pics){
            imageViewSize = [HRFollowDynamicPhotoView sizePhotoViewWithPhotoCount:[_dynamicModel.pics count] andToWidth:backViewWidth];
            _dynamicPhotoViewFrame = CGRectMake(0, CGRectGetMaxY(_contentRect)+leftPading, imageViewSize.width, imageViewSize.height);
        }else{
            _dynamicPhotoViewFrame = CGRectMake(leftPading, CGRectGetMaxY(_contentRect), imageViewSize.width, 0);
        }
        
        backViewHeight = CGRectGetMaxY(_dynamicPhotoViewFrame)+leftPading;
        
    }else if ([_dynamicModel.MomentType intValue] == 1){
        _soundButtonRect = CGRectMake(leftPading, CGRectGetMaxY(_contentRect)+leftPading, 170, 50);
        backViewHeight = CGRectGetMaxY(_soundButtonRect)+leftPading;

    }else if ([_dynamicModel.MomentType intValue] == 4){
//        _videoPlayViewRect = CGRectMake(_picPading*2, CGRectGetMaxY(_contentRect), _backImageWidth-_picPading*3, _backImageWidth-_picPading*3);
//        _picHeight = _videoPlayViewRect.size.height;
//
//        _timeRect = CGRectMake(_picPading*2, CGRectGetMaxY(_videoPlayViewRect)+_picPading,140, 30);
//
//        _backImageViewRect = CGRectMake(_marginLeft, _marginTop, self.backImageWidth, CGRectGetMaxY(_timeRect));

//        [self _latoutBackImage];
    }else if ([_dynamicModel.MomentType intValue] == 5){
        _soundAndImageViewFrame = CGRectMake(0, CGRectGetMaxY(_contentRect)+leftPading, backViewWidth, 120);
        backViewHeight = CGRectGetMaxY(_soundAndImageViewFrame)+leftPading;

    }
    
    
    _bottomViewRect = CGRectMake(0, backViewHeight, backViewWidth, 30);
    
    
    _contentViewRect = CGRectMake(leftPading, CGRectGetMaxY(_timeViewRect)+leftPading, backViewWidth, CGRectGetMaxY(_bottomViewRect));
    
    _cellHeight = CGRectGetMaxY(_contentViewRect)+leftPading;
    

    
//    _cellHeight = CGRectGetMaxY(_backImageViewRect)+_marginTop;
//
//    _lineRect = CGRectMake(_marginLeft/2, 0, 1, _cellHeight);
//
//    _circleRect = CGRectMake(_marginLeft/2-7.5, _cellHeight-_marginTop*2, 15, 15);
    
    
    
}

//- (void)_latoutBackImage{
//    _backImageViewRect = CGRectMake(_marginLeft, _marginTop, self.backImageWidth, _picHeight+_picPading*5+_contentRect.size.height);
//}
//
//
//- (void)_layoutPics{
//    _picSize = CGSizeZero;
//    _picHeight = 0;
//    if (_dynamicModel.pics.count == 0) return;
//
//    _picSize = CGSizeMake(_backImageWidth-_picPading*3, _backImageWidth-_picPading*3-40);
//    _picHeight = _dynamicModel.pics.count*(_picSize.height+_picPading)+_picPading;
//
//}


- (CGFloat )layoutTitleHeight{
    NSString *contentText = _dynamicModel.MomentNotes;
    if(contentText.length == 0) return 0;
    
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
    paraStyle.lineSpacing = 8;
    NSDictionary *dic = @{NSFontAttributeName:CustomUIFont(13), NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@0.0f
                          };
    return [_dynamicModel.MomentNotes boundingRectWithSize:CGSizeMake(kFullWidth - self.leftPading*4, HUGE) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size.height+20;

}

//- (void)_layoutDetailTitle{
//    NSString *contentText = _dynamicModel.MomentNotes;
//    if(contentText.length == 0) return;
//
//
//    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
//    paraStyle.lineSpacing = 8;
//    NSDictionary *dic = @{NSFontAttributeName:CustomUIFont(13), NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@1.5f
//                          };
//    _textHeight = [_dynamicModel.MomentNotes boundingRectWithSize:CGSizeMake(kFullWidth - leftPading*4, HUGE) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size.height+20;
//}

@end
